using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBSourceToolsLib.DatabaseProjectUI
{
    public partial class NewDatabaseForm : Form, Interfaces.IDatabaseInfo
    {
        private bool m_bMustTestConnection;
        private DBConnectionLib.Database m_Database;
        private bool m_bIsNunitTest;

        //public DBConnectionLib.Database Database
        //{
        //    get { return m_Database; }
        //    set { m_Database = value; }
        //}

        public NewDatabaseForm(bool bMustTestConnection, bool bIsNunitTest)
        {
            InitializeComponent();
            this.m_bMustTestConnection = bMustTestConnection;
            this.m_bIsNunitTest = bIsNunitTest;
        }

        private void NewDatabaseForm_Load(object sender, EventArgs e)
        {
            this.m_comboBoxAuthentication.SelectedIndex = 0;
            EnableButtons(0);
            this.btnOk.Enabled = !m_bMustTestConnection;
            EnableControls();
        }

        private void EnableControls()
        {
            if (String.IsNullOrEmpty(this.m_textBoxNickName.Text))
            {
                this.m_textBoxDatabaseName.Enabled = false;
                this.m_comboBoxAuthentication.Enabled = false;
                this.m_textBoxServerName.Enabled = false;
            }
        }

        private void m_comboBoxAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            EnableButtons(this.m_comboBoxAuthentication.SelectedIndex);
        }

        private void EnableButtons(int selIdx)
        {
            if (selIdx == 0)
            {
                this.m_textBoxPassword.Enabled = false;
                this.m_textBoxUserName.Enabled = false;
            }
            else
            {
                this.m_textBoxPassword.Enabled = true;
                this.m_textBoxUserName.Enabled = true;
            }
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            TestConnection(!this.m_bIsNunitTest);
        }

        private bool TestConnection(bool bShowSuccess)
        {
            try
            {
                if (CheckData())
                {
                    DBConnectionLib.Database oDatabase = new DBConnectionLib.Database(
                        this.m_textBoxDatabaseName.Text, this.m_textBoxServerName.Text, this.m_textBoxDatabaseName.Text, "");

                    if (this.m_comboBoxAuthentication.SelectedIndex == 1)
                    {
                        oDatabase.UserName = this.m_textBoxUserName.Text;
                        oDatabase.Password = this.m_textBoxPassword.Text;
                    }

                    if (oDatabase.TestSmoConnection())
                    {
                        if (oDatabase.IsDbOwner())
                        {
                            if (bShowSuccess)
                                ShowMessage("Connection succeeded.");
                            this.m_Database = null;
                            this.m_Database = oDatabase;
                            this.btnOk.Enabled = true;
                        }
                        else
                        {
                            ShowMessage("This user is not in the db_owner group for " + oDatabase.DatabaseName);
                            this.btnOk.Enabled = false;
                            return false;
                        }
                    }
                    else
                    {
                        ShowErrorMessage("Connection failed." + Environment.NewLine + oDatabase.ErrorMessage);
                        return false;
                    }

                }
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex.ToString());
                return false;
            }
            return true;
        }

        private void ShowErrorMessage(string strMessageText)
        {
            if (!this.m_bIsNunitTest)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(strMessageText);
                oError.ShowDialog(this);
            }
        }

        private void ShowMessage(string strMessageText)
        {
            if (!this.m_bIsNunitTest)
            {
                MessageBox.Show(strMessageText);
            }
        }

        private bool CheckData()
        {
            if (String.IsNullOrEmpty(this.m_textBoxServerName.Text))
            {
                ShowMessage("Please specify a server name.");
                return false;
            }
            if (String.IsNullOrEmpty(this.m_textBoxDatabaseName.Text))
            {
                ShowMessage("Please specify a database name.");
                return false;
            }
            // check that this nick is unique:
            if (UserSettings.Instance().DatabaseProject != null)
            {
                DatabaseEx oDbCheck = UserSettings.Instance().DatabaseProject.GetDatabaseEx(m_textBoxNickName.Text);
                if (oDbCheck != null)
                {
                    ShowMessage("This datase nick name is already used in this project.");
                    return false;
                }
            }

            return true;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (CheckData())
            {
                DBConnectionLib.Database oDatabase = new DBConnectionLib.Database(
                    this.m_textBoxNickName.Text, this.m_textBoxServerName.Text, this.m_textBoxDatabaseName.Text, "");

                if (this.m_comboBoxAuthentication.SelectedIndex == 1)
                {
                    oDatabase.UserName = this.m_textBoxUserName.Text;
                    oDatabase.Password = this.m_textBoxPassword.Text;
                }
                this.m_Database = null;
                this.m_Database = oDatabase;
                this.DialogResult = DialogResult.OK;
                this.Close();

            }

        }


        #region IDatabaseInfo Members

        public DBConnectionLib.Database GetDatabaseObject()
        {
            return this.m_Database;
        }

        #endregion

        private void m_textBoxNickName_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(m_textBoxNickName.Text))
            {
                this.m_textBoxServerName.Enabled = true;
            }
            else
            {
                this.m_textBoxServerName.Enabled = false;
            }
            if (!String.IsNullOrEmpty(m_textBoxServerName.Text))
            {
                this.m_textBoxDatabaseName.Enabled = true;
                this.m_comboBoxAuthentication.Enabled = true;
            }
            else
            {
                this.m_textBoxDatabaseName.Enabled = false;
                this.m_comboBoxAuthentication.Enabled = false;
            }

        }

        private void m_textBoxServerName_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(m_textBoxServerName.Text))
            {
                this.m_textBoxDatabaseName.Enabled = true;
                this.m_comboBoxAuthentication.Enabled = true;
            }
            else
            {
                this.m_textBoxDatabaseName.Enabled = false;
                this.m_comboBoxAuthentication.Enabled = false;
            }
        }

        private void m_textBoxDatabaseName_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.m_textBoxDatabaseName.Text))
            {
                this.btnTestConnection.Enabled = true;
                //if (this.m_bMustTestConnection)
                this.btnOk.Enabled = (!this.m_bMustTestConnection);
            }
            else
            {
                this.btnTestConnection.Enabled = false;
                this.btnOk.Enabled = false;
            }
        }

        private void m_btnDatabases_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(this.m_textBoxServerName.Text))
                {
                    ShowMessage("Please specify a server name.");
                    return;
                }

                DBConnectionLib.Database oDatabase = new DBConnectionLib.Database(
                    "", this.m_textBoxServerName.Text, "", "");

                if (this.m_comboBoxAuthentication.SelectedIndex == 1)
                {
                    oDatabase.UserName = this.m_textBoxUserName.Text;
                    oDatabase.Password = this.m_textBoxPassword.Text;
                }

                // test that we can connect to this server
                // this will throw an exception
                Microsoft.SqlServer.Management.Smo.Server oServer = oDatabase.GetSmoServer();
                int iCount = oServer.Databases.Count;

                DatabaseListForm oListForm = new DatabaseListForm(oDatabase);
                if (oListForm.ShowDialog(this) == DialogResult.OK)
                    this.m_textBoxDatabaseName.Text = oListForm.DatabaseName;

            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex.ToString());
            }
        }
    }
}